<?php

	include_once ( "AbstractDAO.php" );

	class CategoryDAO extends AbstractDAO
	{
		//Get Level 1 shop Category from table product_category
		function GetShopLevel1Cate()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "topLevelShopCate", CNS_INDEX );
			if( $re !== false )
				return $re;
			
			$result = $this->db->GetPlan( "select id, cate_name from shop_category where parent_id=0" );
			$shopCateList = array();
			foreach ( $result as $item )
			{
				$shopCateList[$item['cate_name']]=$item['id'];
		    }
		    $cacheManager->Set( "topLevelShopCate", $shopCateList, CNS_INDEX );
		    return $shopCateList;
		}
		
		function GetShopCateNameByCid($scid)
		{
			$result=$this->db->GetValue("select cate_name from shop_category where id=".$scid);
			if(!$result)
				return '';
			else
				return $result;
		}
		
		function GetShopCateIDByName($scName)
		{
			$result=$this->db->GetValue("select id from shop_category where cate_name='$scName'");
			return $result;
		}
		
		/**
		 * Convert taobao-cid to Local Cid
		 */
		function ConvertTaobaoShopCid($taobao_cid)
		{
			$result=$this->db->GetValue("select id from shop_category where taobao_cid=".$taobao_cid);
			if(!$result)
				return '0';
			else
				return $result;
		}
		
		//Get Level 1 product Category from table product_category
		function GetProductLevel1Cate()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "topLevelProductCate", CNS_INDEX );
			if( $re !== false )
				return $re;
				
			$result = $this->db->GetPlan( "select id, cate_name from product_category where parent_id=0" );
			$productCateList = array();
			foreach ( $result as $item )
			{
				$productCateList[$item['cate_name']]=$item['id'];
		    }
		    $cacheManager->Set( "topLevelProductCate", $shopCateList, CNS_INDEX );
		    return $productCateList;
		}
		
		function GetProductCateIDByName($pcName)
		{
			$result=$this->db->GetValue("select id from product_category where cate_name='$pcName'");
			return $result;
		}
		
		//Map from product category id to category name
		function GetProductCateNameByCid($pcid)
		{
			$result=$this->db->GetValue("select cate_name from product_category where id=".$pcid);
			if(!$result)
				return '';
			else
				return $result;
		}

		/**
		 * return category names by category id list
		 * @param array $ids
		 * @return array
		 */
		function GetProductCateByCidList( $ids )
		{
			$strCateIds = implode( ",", $ids );
			$result = $this->db->GetPlan( "select id, cate_name from product_category where id in (".$strCateIds.") 
			ORDER BY FIELD(id, ".$strCateIds.")" );
			return $result;
		}
		
		function ConvertTaobaoProductCid($taobao_cid)
		{
			$result=$this->db->GetRow("select id, parent_id from product_category where taobao_cid=".$taobao_cid);
			if($result)
			{
				if($result['parent_id']=='0')
				{
					return $result['id'];
				}
				while($result['parent_id']!='0')
				{
					$result=$this->db->GetRow("select id, parent_id from product_category where id=".$result['parent_id']);
				}
				return $result['id']; 				
			}
			else
			   return '0';//找不到返回0
	    }
	} 